package com.pilates

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Set;

import com.pilates.financeiro.FinanceiroUtil;
import com.pilates.financeiro.entity.Financeiro 
import com.pilates.financeiro.entity.FinanceiroPeriodo;

class FinanceiroController {

    def index = {
		Financeiro fin = null;
		List<FinanceiroPeriodo> lFinanceiroPeriodoTotal = new ArrayList<FinanceiroPeriodo>();
		Date dataInicial;
		Date dataFinal;
		println  "ini: "+params.dataIni
		println "fin: "+params.dataFin
		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
		if(params.dataIni == "" || params.dataFin == ""){
			Calendar cal = Calendar.getInstance();
			cal.add(Calendar.MONTH, -1);
			dataInicial = cal.getTime();
			
			cal = Calendar.getInstance();
			cal.add(Calendar.MONTH, 1);
			dataFinal = cal.getTime();
		}else{
			try{
				dataInicial = sdf.parse(params.dataIni);
				dataFinal = sdf.parse(params.dataFin);
			}catch (Exception e) {
				Calendar cal = Calendar.getInstance();
				cal.add(Calendar.MONTH, -1);
				cal.set(Calendar.DATE, 1);
				dataInicial = cal.getTime();
				
				cal = Calendar.getInstance();
				cal.set(Calendar.DATE, 1);
				cal.add(Calendar.MONTH, 1);
				dataFinal = cal.getTime();
			}
		}
		
		FinanceiroUtil financeiro = new FinanceiroUtil();
		
		
		
		fin = financeiro.geraFinanceiro(dataInicial, dataFinal);
		lFinanceiroPeriodoTotal.addAll(fin.getlFinanceiroPeriodoTotal());
	
		[financeiro:  fin, periodos: lFinanceiroPeriodoTotal, dataIniStr : sdf.format(dataInicial.getTime()), dataFinStr : sdf.format(dataFinal.getTime()), dataIni : dataInicial, dataFin : dataFinal]
	}
}
